<?php
namespace Admin\Controller;

class SiterStatsController extends Controller {

    public function index() {
        $m    = D('VSiterStats');
        $page = $m->where($this->indexExpr)->order($this->sortExpr)->page2();

        if(!empty($this->indexExpr['uid'])) {
            if($last7DayStatsRecord = $this->getLast7DayStatsRecord($this->indexExpr['uid'])) {
                $this->assign('last7DayStats', $last7DayStatsRecord);
            }
        }

        $this->assign('page', $page);
        $this->display();
    }

    /**
     * getLast7DayStatsRecord
     * 获取最近7天的结算数据
     *
     * @return void
     */
    protected function getLast7DayStatsRecord($uid) {
        $uid = (int)$uid;
        $m   = D2('VSiterStats');
        $r   = array();

        for($i = -1; $i >= -7; $i--) {
            $day  = date('Y-m-d', strtotime("{$i} day"));
            $expr = array(
                'uid' => $uid,
                'day' => $day,
            );

            if($tmp = $m->where($expr)->find()) {
                $tmp['day'] = $day;
                $r[]        = $tmp;
            }
        }

        return $r;
    }

    /**
     * index2
     * 投放指定计划的站长统计信息
     *
     * @return void
     */
    public function index2() {
        parent::_before_index();

        $m    = D('SiterStats');
        $page = $m->where($this->indexExpr)->order($this->sortExpr)->page2();
        echo $m->getlastsql();

        $this->assign('page', $page);
        $this->display();
    }

    /**
     * index3
     * 统计最近一个月有满百记录的站长
     *
     * @return void
     */
    public function index3() {
        $m        = D2('VSiterStats');
        $startDay = strtotime('-1 month');
        $endDay   = strtotime('-1 day');
        $day      = $startDay;
        $uids     = array();

        $loginUser = C('RUN_LOGIN_USER');
        $serviceId  = $loginUser['type'] == 3 ? $loginUser['uid'] : null;

        do {
            $_day = date('Y-m-d', $day);
            $day  = strtotime('+1 day', $day);
            $expr = array('day' => $_day);

            if(!empty($serverId)) {
                $expr['service_id'] = $serviceId;
            }

            if(!$tmp = $m->where($expr)->order('siter_income DESC')->limit(10)->select()) {
                continue;
            }

            foreach($tmp as $v) {
                $uids[] = $v['uid'];
            }
        } while($day <= $endDay);

        $uids = array_unique($uids);

        if(empty($uids)) {
            $this->display();
            exit();
        }

        $day     = $startDay;
        $records = array();

        do {
            $_day = date('Y-m-d', $day);
            $day  = strtotime('+1 day', $day);
            $expr = array('day' => $_day, 'uid' => array('IN', $uids));

            if(!$tmp = $m->field('uid, siter_income AS money')->where($expr)->select()) {
                continue;
            }

            $records[$_day] = $tmp;
        } while($day <= $endDay);

        $days = array();
        $dao  = array();

        foreach($records as $k => $v) {
            $_day   = $k;
            $days[] = $_day;

            foreach($v as $v2) {
                $_uid              = $v2['uid'];
                $dao[$_uid][$_day] = (float)$v2['money'];
            }
        }

        /**
         * 按日期补全收入数据，让每个会员在$days对应的所有日期都有收入数据
         */
        foreach($uids as $k => $v) {
            foreach($days as $v2) {
                if(!isset($dao[$v][$v2])) {
                    $dao[$v][$v2] = 0;
                }
            }

            ksort($dao[$v]);
        }

        $r = array('days' => $days, 'dao' => $dao);
        $this->assign($r)->display();
    }

    public function index4() {
        $m        = D2('VSiterStats');
        $startDay = strtotime('-7 day');
        $endDay   = strtotime('-1 day');
        $day      = $startDay;
        $uids     = array();
        $limit    = 30;

        $loginUser = C('RUN_LOGIN_USER');
        $serviceId  = $loginUser['type'] == 3 ? $loginUser['uid'] : null;

        do {
            $_day = date('Y-m-d', $day);
            $day  = strtotime('+1 day', $day);
            $expr = array('day' => $_day);

            if(!empty($serverId)) {
                $expr['service_id'] = $serviceId;
            }

            if(!$tmp = $m->field('uid')->where($expr)->order('siter_income DESC')->limit($limit)->select()) {
                continue;
            }

            foreach($tmp as $v) {
                $uids[] = $v['uid'];
            }
        } while($day <= $endDay);

        $uids = array_unique($uids);

        if(empty($uids)) {
            $this->display();
            exit();
        }

        $nextDay = $startDay;
        $records = array();

        do {
            $_day    = date('Y-m-d', $nextDay);
            $_expr   = ['day' => $_day, 'uid' => ['IN', $uids]];
            $nextDay = strtotime('+1 day', $nextDay);

            if (!$tmp = $m->field('uid, siter_income AS money')->where($_expr)->select()) {
                continue;
            }

            $records[$_day] = $tmp;
        } while($nextDay <= $endDay);

        $days = array();
        $dao  = array();

        /**
         * 将数据集转换成以uid为第一维，日期为第二维的数组
         */
        foreach ($records as $k => $v) {
            $_day   = $k;
            $days[] = $_day;

            foreach ($v as $v2) {
                $_uid              = $v2['uid'];
                $dao[$_uid][$_day] = (float)$v2['money'];
            }
        }

        $days = array_unique($days);

        /**
         * 按日期补全收入数据，让每个会员在$days对应的所有日期都有收入数据
         */
        foreach ($uids as $_uid) {
            foreach ($days as $_day) {
                if (!isset($dao[$_uid][$_day])) {
                    $dao[$_uid][$_day] = 0;
                }
            }

            ksort($dao[$_uid]);
        }

        $this->assign([
            'days'    => $days,
            'records' => $dao
        ])->display();
    }

}
